<?php
$g4_path = '../';
require_once ($g4_path.'/common.php');
require_once ('./admin.lib.php');

global $cate1, $cate2, $cate3, $cate4, $_isUseCate4; 

$_catesql="SELECT cate1 v FROM book_category WHERE cate1 != '백업' GROUP BY cate1"; 
$list1 = fetchAll2($_catesql, $cate1);
array_unshift($list1, array('','--대분류--'));

if($cate1) {
    $_catesql="SELECT cate2 v FROM book_category WHERE cate1 = '".$cate1."' GROUP BY cate2"; 
    $list2 = fetchAll2($_catesql, $cate2);
    array_unshift($list2, array('','--선택해주세요--'));
} else {
    $list2 = array(array('','--중분류--','')); 
}

if($cate1 && $cate2) {
    $_catesql="SELECT cate3 v FROM book_category WHERE cate1 = '".$cate1."' AND cate2 = '".$cate2."' GROUP BY cate3";
    $list3 = fetchAll2($_catesql, $cate3);
    array_unshift($list3, array('','--선택해주세요--'));
} else {
    $list3 = array(array('','--소분류--','')); 
}

if($_isUseCate4) {
        $_sqlWhere = NULL; 
        if($cate1) {
            $_sqlWhere.= " cate1 = '".$cate1."' "; 
        }
        if($cate2) {
            if($_sqlWhere) {
                $_sqlWhere.= ' AND '; 
            }
            $_sqlWhere.= " cate2 = '".$cate2."' "; 
        }
        if($cate3) {
            if($_sqlWhere) {
                $_sqlWhere.= ' AND '; 
            }
            $_sqlWhere.= " cate3 = '".$cate3."' "; 
        }

        if($_sqlWhere) {
            $_sqlWhere = ' WHERE '.$_sqlWhere; 
        }

    $_catesql="SELECT name as v, no as n FROM book ".$_sqlWhere;
    $list4 = fetchAll2($_catesql, $cate4);
    array_unshift($list4, array('','--교재 선택이 가능합니다--'));
} else {
    $list4 = array(array('','--교재 이름--','')); 
}
?>
<script type="text/javascript" src="inc/chain_select.js"></script>
<script type="text/javascript">
var category_data ={
        'cate1':<?php echo json_encode2($list1); ?>,
        'cate2':<?php echo json_encode2($list2); ?>,
        'cate3':<?php echo json_encode2($list3); ?>
<?php
    if($_isUseCate4) {
        echo ','.'\'cate4\':'.json_encode2($list4);
    }
?>
};
</script>
<script type="text/javascript">
    var category = new ChainSelect(category_data,'inc/book_category_list.php', <?php echo "'$_isUseCate4'"; ?> );
</script>
